package com.leoman.work.service.impl;

import com.leoman.category.dao.CategoryDao;
import com.leoman.category.entity.Category;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.FileBo;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.utils.FileUtil;
import com.leoman.welfare.dao.WelfareDao;
import com.leoman.work.dao.WorkCreatorDao;
import com.leoman.work.dao.WorkDao;
import com.leoman.work.dao.WorkSurroundDao;
import com.leoman.work.entity.Work;
import com.leoman.work.entity.WorkCreator;
import com.leoman.work.entity.WorkSurround;
import com.leoman.work.service.WorkSurroundService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.io.IOException;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/6/17 0017.
 *
 */
@Service
public class WorkSurroundServiceImpl extends GenericManagerImpl<WorkSurround, WorkSurroundDao> implements WorkSurroundService {

    @Autowired
    private WorkSurroundDao workSurroundDao;

    @Autowired
    private WorkDao workDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private WorkCreatorDao workCreatorDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private WelfareDao welfareDao;

    @Override
    public List<WorkSurround> findByWorkId(Long workId) {
        return workSurroundDao.findByWorkId(workId);
    }

    @Override
    @Transactional
    public void saveAll(Long id,
                        String name,
                        Integer seriesCount,
                        Long category,
                        String description,
                        String[] creator,
                        String[] type,
                        String[] welfareList,
                        String[] types,
                        String[] welfareLists,
                        MultipartRequest multipartRequest) throws IOException {

        Work work;

        if (null == id) {
            work = new Work();
            work.setCreateDate(System.currentTimeMillis());
            work.setPlayNum(0);
            work.setBarrageNum(0);
        } else {
            work = workDao.findOne(id);

            // 先删除原有的分类数量
            /*Category category1 = work.getCategory();
            if (category1 != null) {
                category1.setCount(category1.getCount() - 1);
                categoryDao.save(category1);
            }*/
        }

        work.setName(name);
        work.setSeriesCount(seriesCount);

        if (category != null) {
            // 后新增分类数量
            /*Category category1 = categoryDao.findOne(category);
            category1.setCount(category1.getCount() + 1);
            categoryDao.save(category1);*/

            work.setCategory(categoryDao.findOne(category));
        }

        work.setDescription(description);
        workDao.save(work);
        MultipartFile multipartFile = multipartRequest.getFile("imageFile");
        if (null != multipartFile) {
            FileBo fileBo = FileUtil.save(multipartFile, true);
            work.setCover(fileBo.getPath());
            work.setSuolue(fileBo.getSuoluePath());
        }
        workDao.save(work);

        //List<WorkCreator> workCreatorList = workCreatorDao.findWorkId(id);

        // 保存改作品的主创
        for (int i = 0; i < creator.length - 1; i++) {
            WorkCreator workCreator = new WorkCreator();
            workCreator.setWork(work);
            workCreator.setUserInfo(userInfoDao.findOneByNickname(creator[i]));
            workCreator.setPraise(0);
            workCreatorDao.save(workCreator);
        }

        //修改周边
        List<WorkSurround> workSurroundList = workSurroundDao.findByWorkId(work.getId());
        if (workSurroundList.size() > 0) {
            for (int i = 0; i < types.length; i++) {
                if (StringUtils.isNotEmpty(types[i])) {
                    workSurroundList.get(i).setType(Integer.parseInt(types[i]));
                    workSurroundList.get(i).setWork(work);
                    if (StringUtils.isNotEmpty(welfareLists[i])) {
                        workSurroundList.get(i).setWelfare(welfareDao.findOne(Long.parseLong(welfareLists[i])));
                    }
                    workSurroundDao.save(workSurroundList.get(i));
                }
            }
        }

        //新增周边
        for (int i = 0; i < type.length - 1; i++) {
            if (StringUtils.isNotEmpty(type[i])) {
                WorkSurround workSurround = new WorkSurround();
                workSurround.setType(Integer.parseInt(type[i]));
                workSurround.setWork(work);
                if (StringUtils.isNotEmpty(welfareList[i])) {
                    workSurround.setWelfare(welfareDao.findOne(Long.parseLong(welfareList[i])));
                }
                workSurroundDao.save(workSurround);
                //workSurroundList.add(workSurround);
            }
        }
    }

}
